<?php
/**
 * User: Drery
 * Date: 2015/12/28
 * Time: 10:16
 */

namespace backend\components;

use backend\models\Permission;
use Yii;
use yii\filters\AccessRule;
use yii\web\NotFoundHttpException;


class PermissionAccess extends AccessRule
{
    public function allows($action, $user, $request)
    {
        $service = $action->controller->id.'/'.$action->id;
        $Permission = Permission::find()->where(['link'=>$service,'is_deleted'=>0])->one();
        if (!$Permission) {
            throw new NotFoundHttpException;
        }
        Yii::$app->controller->permission = $Permission;
        if ($user->identity->user_name == 'root') {
            return true;
        }
        if (in_array($Permission->id,$user->identity->permissionIds)) {
            return true;
        }
        return false;
    }
}